<?php
require_once('PHPUnit/Extensions/Database/TestCase.php');
require_once('./includes/phpunit_setup.inc');
require_once('./includes/database.fnsql.inc');
require_once('kgallery.admin.inc');

class kgallery_module_Test extends DrupalTddTestCase {
  function setup() {
    parent::setup();
    drupal_install_modules(array('kasahorow', 'kentry', 'kgallery', 'devel'));
    $this->image1 = new stdClass();
    $this->image1->fid = 1;
    $this->image1->filename = 'image1.jpg';
    $this->image1->filemime = 'image/jpeg';
    $this->image1->filepath = 'image1.jpg';
    $this->child1 = new stdClass();
    $this->child1->kfid = 1;
    $this->child1->value = 'http://a/b.png';
    $this->node = new stdClass();
    $this->node->nid = 932;
    $this->node->iso = 'test';
    $this->node->files = array();
  }

  function testOnePhotoShouldAutomaticallyBecomeThumbNail() {
    $this->node->files[] = $this->image1;
    $thumbnail = _kgallery_thumbnail_url($this->node, False);
    $this->assertEquals(file_create_url('image1.jpg'), $thumbnail,
                        'First photo should be thumbnail');
  }

  function testChildPhotoAsDefaultThumbnail() {
    $child_value = 'http://a/b.png';
    $this->node->chn[$this->child1->kfid] = $this->child1;
    variable_set('xspf_'. $this->node->type .'_thumb', $this->child1->kfid);
    $this->node->files[] = $this->image1;
    $thumbnail = _kgallery_thumbnail_url($this->node, False);
    $this->assertEquals($child_value, $thumbnail, 
                        'Child value should be thumbnail');
  }

  function testGeoPointAsThumbnail() {
    $this->node->geopoint = '0,0';
    $map_url = 'http://maps.google.com/maps/api/staticmap?size=200x200&sensor=false&center=0,0&span=&hl=&maptype=hybrid&markers=color:black|0,0';
    $thumbnail = _kgallery_thumbnail_url($this->node, False);
    $this->assertEquals($map_url, $thumbnail, 'Google Maps thumbnail');
  }

  function testNodeWithNoFilesOrChildrenReturnsNoJSPlayer() {
    $this->assertEquals(null, _kgallery_js_player($this->node), 
                        'No file/chn, no player');
    $this->node->files[] = $this->image1;
    $this->assertNotNull(_kgallery_js_player($this->node),
                         'File, so player');
    unset($this->node->files);
    $this->assertEquals(null, _kgallery_js_player($this->node),
                        'No file/chn, no player');
    $this->node->chn[$this->child1->kfid] = $this->child1;
    $this->assertNotNull(_kgallery_js_player($this->node),
                         'Child, so player');
  }

  function testNoImageDefaultsToGreetingCardThumbnail() {
    $thumbnail = _kgallery_thumbnail_url($this->node, False);
    $nid = $this->node->nid;
    $this->assertEquals(url("node/$nid/greeting", array('absolute' => 1)),
                        $thumbnail,
                        'Should return greeting card URL as default thumbnail');
  }

  function testFirstImageURLAttachmentIsThumbnailIfNoFiles() {
    $this->testNoImageDefaultsToGreetingCardThumbnail();
    $urls = "http://url1.png\nhttp://url2.jpg";
    $this->node->_autofield = $urls;
    $thumbnail = _kgallery_thumbnail_url($this->node, False);
    $this->assertEquals("http://url1.png", $thumbnail,
                        'Should use first image URL as default thumbnail');
    global $conf; $conf['xspf_playlist_thumb'] = 'http://static.fienipa.com/sites/all/themes/newfn/logo_movies.png';
    $thumbnail = _kgallery_thumbnail_url($this->node, False);
    $this->assertEquals("http://url1.png", $thumbnail,
                        'Still use first image URL as default thumbnail even if default is set');

  }

   function testNodeFileItems() {
    $urls = "http://url1\nhttp://url2";
    $this->node->pass = true; # pretend this is a usernode
    $this->node->files[] = $this->image1;
    variable_set('xspf_playlist_'. $this->node->type .'_fields', array($this->child1->kfid));
    $this->node->chn[$this->child1->kfid] = $this->child1; 
    $this->node->_autofield = $urls;

    $items = theme('xspf_playlist_create_item', $this->node); 
    $this->assertEquals(4, count($items), 'One file item per regular file, chn and 2 _auto field URLs');
  }

  function testNodeTypeThumbNail() {
    variable_set('xspf_playlist_thumb', 'default_thumb.png');
    variable_set('kentry_'.$this->node->iso.'_dthumb', 'http://somewhere'); 
    $thumbnail_url = _kgallery_thumbnail_url($this->node, False);
    $this->assertEquals('http://somewhere', $thumbnail_url, 'Expecting nodetype thumbnail url');
  }

  function testAddMediaToGallery() {
    $url = 'http://yahoo.com/as.mp3';
    $name = 'Go rock it';
    $type = 'mp3';
    $mid = _kgallery_add($url, $type, $name);
    $media = _kgallery_get($mid);
    $this->assertEquals($url, $media->url);
    $this->assertEquals($name, $media->name);
    $this->assertEquals($type, $media->type);

  }
}

